/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

/*
 * @Author: 武兴创 superjava@qq.com
 * @Date: 2024-08-01 19:53:40
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-10 17:06:54
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\BulkDataEntryParserFactory.h
 */
#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "Import/ImportAPI/Common/ImportNamespace.h"

/**
 * @brief 注册解析Bulk块的Nastran函数。
 *
 * @param ENTRYNAME 词条名称，ex:CBAR
 */

#define BULKDATA_ENTRY_PARSER_REG(ENTRYNAME)                                                                \
    namespace                                                                                               \
    {                                                                                                       \
        bool register_##ENTRYNAME = SG::Import::BDF::BulkDataEntryParserFactory::GetInstance ()->Register ( \
            #ENTRYNAME, &SG::Import::BDF::BULKDATA::Parse##ENTRYNAME);                                      \
    }

/**
 * @brief 解析BULK块的Nastran词条函数。
 *
 * @param dbServiceFactory 数据库服务工厂的共享指针，用于数据库操作。
 * @param EntryInfo 存储条目信息的向量，提供待解析的 BDF 条目。
 * @param ENTRYNAME 词条名称，ex:CBAR。
 * @return 如果解析成功，返回 true；否则返回 false。
 */

#define BULKDATA_ENTRY_PARSER_DECL(ENTRYNAME)                                                                   \
    namespace SG                                                                                                \
    {                                                                                                           \
        namespace Import                                                                                        \
        {                                                                                                       \
            namespace BDF                                                                                       \
            {                                                                                                   \
                namespace BULKDATA                                                                              \
                {                                                                                               \
                    SG::DataStructure::Common::Status                                                           \
                    Parse##ENTRYNAME (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory, \
                                      std::vector<std::string>&                               dividedEntryInfo);                              \
                }                                                                                               \
            }                                                                                                   \
        }                                                                                                       \
    }

DBSERVICEFACTORY_DEC

SG_IMPORT_BDF_NAMESPACE_OPEN

/// @brief 词条解析工厂类
class BulkDataEntryParserFactory
{
  public:
    using NEParseFunP = SG::DataStructure::Common::Status (*) (const std::shared_ptr<SG::DBManager::DBServiceFactory>&,
                                                               std::vector<std::string>&);

    using NEParseMap = std::map<std::string, NEParseFunP>;

    /// @brief 注册解析函数
    /// @param EntryType 词条名称 关键词
    /// @param ParseFunP 解析函数
    /// @return
    bool Register (const std::string& EntryType, NEParseFunP NastranParseSP);

    /// @brief 反注册某解析函数
    /// @param EntryType
    void Unregister (const std::string& EntryType);

    /// @brief 清空所有注册的解析函数
    void UnregisterAll ();

    /// @brief 获取解析函数
    /// @param EntryType
    /// @return
    NEParseFunP GetFunction (const std::string& EntryType);

    /// @brief 单例模式获取实例
    /// @return
    static BulkDataEntryParserFactory* GetInstance ();

  private:
    NEParseMap m_NEParseFunMap;  /// 词条解析函数映射

    BulkDataEntryParserFactory ();
};

SG_IMPORT_BDF_NAMESPACE_CLOSE